package team.epos.utils.util;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;

public class FileUploadUtil {

    public static Map<String, String> getUploadParam(HttpServletRequest request, String path, String realPath) {
        Map<String, String> param = new HashMap<>(16);
        try {
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if (isMultipart) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List<FileItem> fileItems = upload.parseRequest(request);
                for (FileItem item : fileItems) {
                    if (item.isFormField()) {
                        param.put(item.getFieldName(), item.getString("UTF-8"));
                    } else {
                        File file = new File(realPath);
                        if (!file.exists()) {
                            file.mkdir();
                        }
                        String fieldName = item.getFieldName();
                        String name = item.getName();
                        if (StringUtils.isNotBlank(name)) {
                            StringBuilder builder = new StringBuilder();
                            String uuid = UUID.randomUUID().toString();
                            builder.append(path);
                            builder.append(File.separator);
                            builder.append(uuid);
                            builder.append(name);
                            save(item.getInputStream(), realPath + File.separator + uuid + name);
                            param.put(fieldName, builder.toString());
                        }
                    }
                }
                return param;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return param;
    }

    private static void save(InputStream in, String path) throws IOException {
        OutputStream outputStream = new FileOutputStream(path);
        int len;
        while ((len = in.read()) != -1) {
            outputStream.write(len);
        }
        outputStream.flush();
        outputStream.close();
        in.close();
    }


    public static String img2Base64(String path) {
        String res = null;
        try {
            File file = new File(path);
            InputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int len;
            while ((len = in.read()) != -1) {
                out.write(len);
            }
            byte[] result = out.toByteArray();
            res = Base64.encodeBase64String(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static void base64ToFile(String path, String img) {
        byte[] buf = Base64.decodeBase64(img);
        try {
            OutputStream out = new FileOutputStream(path);
            out.write(buf);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
